package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarQuestionSelectBean;
import com.das.mechanic_base.bean.main.CrmCarQuetionBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class X3CarQuestionAdapter extends RecyclerView.Adapter<X3CarQuestionAdapter.CarQuestionHolder> {

    private List<CrmCarQuetionBean> mList;
    private List<Boolean> sList;
    private Context mContext;
    private List<CarQuestionSelectBean> selectList;
    private  List<Integer> posList;
    private HashMap<String,String> brandMap;
    public X3CarQuestionAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.posList = new ArrayList<>();
        this.sList = new ArrayList<>();
        this.brandMap = new HashMap<>();

    }
    public void setDataList(List<CrmCarQuetionBean> mList,List<CarQuestionSelectBean> selectLists) {
        this.mList = mList;
        this.sList.clear();
        this.posList.clear();
        this.brandMap.clear();
        notifyDataSetChanged();
        if(!X3StringUtils.isListEmpty(selectLists)){
            for (int i = 0; i < selectLists.size(); i++){
                CarQuestionSelectBean carBean = selectLists.get(i);
                if (brandMap.containsKey(carBean.itemName)) {
                    int num =  Integer.parseInt(brandMap.get(carBean.itemName)) + 1;
                    brandMap.put(carBean.itemName,("ALL".equals(carBean.itemCodes)) ? mContext.getResources().getString(R.string.all) : (num >= 100) ? "99+" : num + "");
                }else {
                    brandMap.put(carBean.itemName,("ALL".equals(carBean.itemCodes)) ? mContext.getResources().getString(R.string.all) : "1");
                }
            }
        }
//        }
    }


    @NonNull
    @Override
    public X3CarQuestionAdapter.CarQuestionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3CarQuestionAdapter.CarQuestionHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_car_question_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3CarQuestionAdapter.CarQuestionHolder holder, int position) {
            holder.tv_name.setText(mList.get(position).itemName);
            holder.tv_num.setText(brandMap.get(mList.get(position).itemName));
            holder.tv_num.setVisibility(X3StringUtils.isEmpty(brandMap.get(mList.get(position).itemName)) ? View.GONE : View.VISIBLE);
            holder.tv_name.setText(mList.get(position).itemName + "");

            holder.itemView.setOnClickListener(v -> {
                if(iOnCarQuestion!=null){
                    iOnCarQuestion.getCarQuesInfo(mList.get(position));
                }
            });
//            if(sList.get(position)){
//                holder.tv_name.setTextColor(Color.parseColor("#0077FF"));
//            }else {
//                holder.tv_name.setTextColor(Color.parseColor("#313340"));
//            }

            if(mList.get(position).itemCode.equals("ENGINE")){//车况
                holder.iv_img.setImageResource(R.mipmap.x3_engine);
            } else if(mList.get(position).itemCode.equals("MOTOR")){
                holder.iv_img.setImageResource(R.mipmap.x3_motor);
            } else if(mList.get(position).itemCode.equals("GEARBOX")){
                holder.iv_img.setImageResource(R.mipmap.x3_gearbox);
            } else if(mList.get(position).itemCode.equals("CHASSIS")){
                holder.iv_img.setImageResource(R.mipmap.x3_chassis);
            } else if(mList.get(position).itemCode.equals("AIRCON")){
                holder.iv_img.setImageResource(R.mipmap.x3_airconditioning);
            } else if(mList.get(position).itemCode.equals("ELECTRIC")){
                holder.iv_img.setImageResource(R.mipmap.x3_electricalequipment);
            } else if(mList.get(position).itemCode.equals("EXTERIOR")){
                holder.iv_img.setImageResource(R.mipmap.x3_exterior);
            } else if(mList.get(position).itemCode.equals("INTERIOR")){
                holder.iv_img.setImageResource(R.mipmap.x3_interior);
            } else if(mList.get(position).itemCode.equals("INTERIOR")){
                holder.iv_img.setImageResource(R.mipmap.x3_interior);
            } else if(mList.get(position).itemCode.equals("MAINTAIN")){ //保养
                holder.iv_img.setImageResource(R.mipmap.x3_maintenance);
            } else if(mList.get(position).itemCode.equals("REPAIR")){
                holder.iv_img.setImageResource(R.mipmap.x3_repair);
            } else if(mList.get(position).itemCode.equals("REPLACE")){
                holder.iv_img.setImageResource(R.mipmap.x3_replace);
            } else if(mList.get(position).itemCode.equals("DETECTION")){
                holder.iv_img.setImageResource(R.mipmap.x3_healthcheck);
            }  else if(mList.get(position).itemCode.equals("METAL_SPRAY")){
                holder.iv_img.setImageResource(R.mipmap.x3_painting);
            }  else if(mList.get(position).itemCode.equals("COSMETIC")){
                holder.iv_img.setImageResource(R.mipmap.x3_detailingwash);
            }  else if(mList.get(position).itemCode.equals("NONSTANDARD")){
                holder.iv_img.setImageResource(R.mipmap.x3_otherques);
            }
    }

    IOnCarQuestionListener iOnCarQuestion;

    public void setiOniOnCarQuestion(IOnCarQuestionListener iOnCarQuestion) {
        this.iOnCarQuestion = iOnCarQuestion;
    }

    public interface IOnCarQuestionListener {
            void getCarQuesInfo(CrmCarQuetionBean crmCarQuetionBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CarQuestionHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_img;
        TextView tv_num;
        public CarQuestionHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_num = itemView.findViewById(R.id.tv_num);

        }
    }
}
